﻿using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Windows.Threading;
using System.Xml.Serialization;
using NCT.Entity;
using System.Collections.Generic;
using WindowsPhonePostClient;

namespace NCT.DataAccessComponents
{
    public class ItemsPreparation : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        ListItem listItem;

        public ListItem ListItem
        {
            get { return listItem; }
            set
            {
                if (listItem != value)
                {
                    listItem = value;
                    OnPropertyChanged("ListItem");
                }
            }
        
        }

        public ItemsPreparation()
        {
            if (listItem == null)
                listItem = new ListItem();
        }


        // Methods get items
        // result ->  listItem
        public void getSearch(int currPage, int perPage, string keyword, string action)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("secretkey", "nct@mobile!team");
            parameters.Add("action", action);
            parameters.Add("keyword", keyword);
            parameters.Add("deviceinfo", NCTConstants.DEVICE_INFOR);
            parameters.Add("curpage", currPage.ToString());
            parameters.Add("perpage", perPage.ToString());

            PostClient proxy = new PostClient(parameters);
            
            proxy.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    //Process the result...
                    ListItem =  XMLAction.ItemsParser(e.Result);
                }
            };
            proxy.DownloadStringAsync(new Uri(NCTConstants.REST_SERVICE_V3_URL, UriKind.Absolute));
        }

        protected virtual void OnPropertyChanged(string propChanged)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
        }
    }
}